package com.wzu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzu.entity.Division;
import com.wzu.mapper.DivisionMapper;
import com.wzu.service.DivisionService;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class DivisionServiceImpl extends ServiceImpl<DivisionMapper, Division> implements DivisionService {
    @Override
    public List<Division> getAllDivisions() {
        return list();
    }
    @Override
    public List<Division> getDivisionsByName(String name) {
        QueryWrapper<Division> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        return list(queryWrapper);
    }
    @Override
    public boolean addDivision(Division division) {
        // code唯一校验
        QueryWrapper<Division> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", division.getCode());
        if (count(queryWrapper) > 0) {
            return false;
        }
        return save(division);
    }
    @Override
    public boolean updateDivision(Division division) {
        QueryWrapper<Division> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", division.getCode()).ne("id", division.getId());
        if (count(queryWrapper) > 0) {
            return false;
        }
        return updateById(division);
    }
    @Override
    public boolean deleteDivision(Long id) {
        return removeById(id);
    }
    @Override
    public List<Division> getDivisions(Integer level, String parentId) {
        QueryWrapper<Division> wrapper = new QueryWrapper<>();
        if (level != null) wrapper.eq("level", level);
        if (parentId != null) wrapper.eq("parent_id", parentId);
        return list(wrapper);
    }
} 